package main

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/mattn/go-colorable"
	DEATH "gopkg.in/vrecan/death.v3"
	"hr/config"
	"hr/router"
	"hr/service"
	"log"
	"net/http"
	"syscall"
	"time"
)

// @title 工具集
// @version 0.1
// @BasePath /
func main() {
	service.Init()
	defer service.Destroy()

	r := gin.Default()

	router.InitRouter(r)

	srv := &http.Server{
		Addr:   "0.0.0.0:"+config.AppConfig.Server.Port,
		Handler: r,
	}
	fmt.Println("*********************服务启动*********************")
	fmt.Println(fmt.Sprintf("*********************http://127.0.0.1:%s*********************",config.AppConfig.Server.Port))
	fmt.Println("*********************服务启动*********************")
	gin.ForceConsoleColor()
	gin.DefaultWriter = colorable.NewColorableStdout()
	go func() {
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("listen: %s\n", err)
		}
	}()

	death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
	_ = death.WaitForDeath()
	log.Println("Shutdown Server ...")


	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		log.Fatal("Server Shutdown:", err)
	}

	log.Println("Server exiting")
}
